<?php

namespace Product\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Json\Json;
use Product\Controller\Index;

class AjaxController extends AbstractActionController{
	protected $ImgProductTable;
	
	public function indexAction(){
		$request = $this->getRequest()->getPost();
		$act = $request['act'];
		
		switch ($act){
			case 'deleteimg':
				return $this->deleteimgAction();
				break;		
			default:
				return $this->response('Không thực hiện được Request');
				break;
		}
		
	}
	
	
	public function deleteimgAction(){
		$request = $this->getRequest()->getPost();
		$id = $request['id'];
		$image_pro = $request['img'];
		$response = $this->getResponse();
		$image = UPLOAD_PATH.'/product/' . $image_pro;
		if (file_exists ( $image ) == true){
			unlink ( $image ); 
			$this->getImgProductTable()->deleteimgpro($id);
			$response->setContent(Json::encode(array('success'=>1)));
		}	
		
		return $response;
	}
	
	public function getImgProductTable()
	{
		if (!$this->ImgProductTable) {
			$sm = $this->getServiceLocator();
			$this->ImgProductTable = $sm->get('Product\Model\ImgProductTable');
		}
		return $this->ImgProductTable;
	}
	
}

?>